﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlExtractor.XPathOutput;
using System.Linq;
using HtmlExtractor;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Xml.XPath;
using System.Xml;
namespace HtmlExtractor.XPathOutput
{
    public class OneManySimpleProcessor: XmlDocOutputPathProcessor
    {
         public OneManySimpleProcessor():base()
          {}
         public OneManySimpleProcessor(IGrouping<List<string>, IPageItem> inputData):base(inputData)
         { }
        public override bool GetResult(ref XDocument outputData)
        {
            base.GetResult(ref outputData);
           
            //Simple&&多个Pageitem  ，产生列表          
            if (InputData.Key.Count == 1 && InputData.Count()>1)
            {
                XPathOutputPathParser p = new XPathOutputPathParser(InputData.Key[0]);
                string unitPaht = p.CompositeXPath;
                XDocument unitElement = XDocument.Parse(outputData.XPathSelectElement(unitPaht,NameSpace).ToString());
                string all=null;
                foreach (SimpleItem s in InputData.Cast<SimpleItem>())
                {
                    EvaluateXPath(ref unitElement, p.XPath, s.Value);
                    //;
                    all += unitElement.ToString(SaveOptions.DisableFormatting);
                }

                outputData.XPathSelectElement(unitPaht,NameSpace).ReplaceWith(XmlHelper.ReadFragment(all));
               
                return true; ;
            }
            return false;
   
        }
        private string GetUnitPath(string outputPath)
        {
            XPathOutputPathParser p = new XPathOutputPathParser(outputPath);
            return p.CompositeXPath;
        }
    }
}
